﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;

namespace GabageFactory.Models
{
    public class DikeReposity : Reposity
    {
        public IEnumerable<Seperator_Dike_Base_Info> ListDikes()
        {
            var dikes = from d in _dataContext.Seperator_Dike_Base_Infos
                            select d;
            foreach (Seperator_Dike_Base_Info d in dikes)
            {
                yield return d;
            }
        }

        public Seperator_Dike_Base_Info GetSeperatorDikeById(int id)
        {
            var dike = from d in _dataContext.Seperator_Dike_Base_Infos
                          where id == d.stack_id
                          select d;
            if (dike != null && dike.Count() > 0)
            {
                return dike.First();
            }
            else
            {
                return null;
            }
        }

        public Seperator_Dike_Base_Info CreateSeperatorDike(Seperator_Dike_Base_Info dike)
        {
            _dataContext.Seperator_Dike_Base_Infos.InsertOnSubmit(dike);
            SubmitChanges();
            return dike;
        }

        public bool DeleteDike(Seperator_Dike_Base_Info dike)
        {
            _dataContext.Seperator_Dike_Base_Infos.DeleteOnSubmit(dike);
            SubmitChanges();
            return true;
        }

        public Seperator_Dike_Base_Info EditDike(Seperator_Dike_Base_Info dike)
        {
            SubmitChanges();
            return dike;
        }

        public Stack_Base_Info GetStackBaseInfoById(int id)
        {
            var stack = from s in _dataContext.Stack_Base_Infos
                       where id == s.stack_id
                       select s;
            if (stack != null && stack.Count() > 0)
            {
                return stack.First();
            }
            else
            {
                return null;
            }
        }

        public Stack_Base_Info CreateStackBaseInfo(Stack_Base_Info stackBaseInfo)
        {
            _dataContext.Stack_Base_Infos.InsertOnSubmit(stackBaseInfo);
            SubmitChanges();
            return stackBaseInfo;
        }

        public bool DeleteStackBaseInfo(Stack_Base_Info stackBaseInfo)
        {
            _dataContext.Stack_Base_Infos.DeleteOnSubmit(stackBaseInfo);
            SubmitChanges();
            return true;
        }

        public Stack_Base_Info EditStackBaseInfo(Stack_Base_Info stackBaseInfo)
        {
            SubmitChanges();
            return stackBaseInfo;
        }

        public IEnumerable<Stack_Base_Layer_Info> ListStackBaseLayerInfos(int stackId)
        {
            var layers = from s in _dataContext.Stack_Base_Layer_Infos
                         where s.stack_id == stackId
                         orderby s.layer_number ascending
                         select s;

            foreach (Stack_Base_Layer_Info d in layers)
            {
                yield return d;
            }
        }

        public void DeleteStackBaseLayerInfos(int stackId)
        {
            var layers = from s in _dataContext.Stack_Base_Layer_Infos
                         where s.stack_id == stackId
                         select s;

            foreach (Stack_Base_Layer_Info d in layers)
            {
                _dataContext.Stack_Base_Layer_Infos.DeleteOnSubmit(d);
            }
            SubmitChanges();
        }

        public void CreateStackBaseLayerInfo(Stack_Base_Layer_Info layer)
        {
            _dataContext.Stack_Base_Layer_Infos.InsertOnSubmit(layer);
            SubmitChanges();
        }

        public string ListStackElAreaBaseInfos(int stackId, int partitionNo)
        {
            var els = from s in _dataContext.Stack_El_Area_Base_Infos
                         where s.stack_id == stackId && s.partition_no == partitionNo
                         orderby s.El ascending
                         select s;

            string str = "";
            if (els != null && els.Count() > 0)
            {
                foreach (Stack_El_Area_Base_Info el in els)
                {
                    str = str + el.El + ":" + el.area + "\n";
                }
            }

            return str;
        }

        public void DeleteStackElAreaBaseInfos(int stackId)
        {
            var els = from s in _dataContext.Stack_El_Area_Base_Infos
                         where s.stack_id == stackId
                         select s;

            foreach (Stack_El_Area_Base_Info el in els)
            {
                _dataContext.Stack_El_Area_Base_Infos.DeleteOnSubmit(el);
            }
            SubmitChanges();
        }

        public void CreateStackElAreaBaseInfo(Stack_El_Area_Base_Info el)
        {
            _dataContext.Stack_El_Area_Base_Infos.InsertOnSubmit(el);
            SubmitChanges();
        }



    }
}